/*
 * Created on 2005-1-14
 */
package org.nanhill.commons.model;

/**
 * @author alin
 * 
 */
public class AutoLongGenerator  {
	/** Should the counter wrap with max value check. */
	private boolean wrap;

	/** The number the generator counts from. */
	private long count;

	public static final boolean DEFUALT_WRAP = false;

	public static final long DEFUALT_COUNT = 0L;

	public boolean getWrap() {
		return wrap;
	}

	public void setWrap(boolean wrap) {
		this.wrap = wrap;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	/**
	 * @param wrap
	 * @param count
	 */
	public AutoLongGenerator(boolean wrap, long count) {
		this.wrap = wrap;
		this.count = count;
	}

	public AutoLongGenerator(boolean wrap) {
		this(wrap, DEFUALT_COUNT);
	}

	public AutoLongGenerator(long count) {
		this(DEFUALT_WRAP, count);
	}

	public AutoLongGenerator() {
		this(DEFUALT_WRAP, DEFUALT_COUNT);
	}

	public long nextSerial() {
		long value;
		if (!wrap) {
			synchronized (this) {
				value = count++;
			}
		} else {
			synchronized (this) {
				if (count == Long.MAX_VALUE) {
					throw new IllegalStateException(
							"The maximum number of identifiers has been reached");
				}
				value = count++;
			}
		}
		return value;
	}
}